import 'package:api/pages/apps/firstCompanyApp/base/base_page.dart';
import 'package:flustars/flustars.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'guide_page.dart';

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends BasePage<IndexPage> {
  bool misFirst;
  bool mIsShowBg = true;
  String imgUrl =
      "https://th.bing.com/th/id/Rb883afdb11dbf64c872ab3930ba4b559?rik=itNWkdpeiX%2fuWg&pid=ImgRaw";

  TimerUtil mTimerUtil;
  num mTick = 0;

  @override
  void initState() {
    super.initState();
    //  mInterval 设置时间间隔 mTotalTime 总时间
    mTimerUtil = new TimerUtil(mInterval: 1000, mTotalTime: 5000);

    // 计时回调
    mTimerUtil.setOnTimerTickCallback((int tick) {
      mTick = tick / 1000;
      setState(() {});
      if (mTick == 0) {
        pushHomePage();
      }
    });
    // 初始化
    // SharedPreferences.setMockInitialValues({});

    // 加载数据缓存
    readCacheData();
  }

  void readCacheData() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    var isFirst = prefs.getBool('flutter_x_is_first') ?? false;
    if (isFirst == null || isFirst == false) {
      // 第一次使用，去引导页
      // 保存使用标识
      await prefs.setBool("flutter_x_is_first", true);
      pushRePage(GuidePage());
    } else {
      // 非第一次使用
      // 显示倒计时
      // 启动倒计时
      mTimerUtil.startCountDown();
    }

    mIsShowBg = true;
    misFirst = isFirst;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Stack(
        children: [
          buildBgWiget(),
          buildTimerProgress(),
          buildLoadingProgress(),
          buildLogo(),
        ],
      ),
    );
  }

  // 构建背景
  buildBgWiget() {
    return mIsShowBg
        ? Image.network(imgUrl,
            height: 800, width: double.infinity, fit: BoxFit.cover)
        : Container();
  }

  // 构建倒计时圆形进度条
  buildTimerProgress() {
    num cirValue = 0;
    if (mTick == 5) {
      cirValue = 0.0;
    } else {
      cirValue = (5.0 - mTick) / 5.0;
    }
    return Positioned(
      right: 20,
      bottom: 20,
      child: InkWell(
        borderRadius: BorderRadius.all(Radius.circular(55)),
        onTap: () {
          // 关闭倒计时
          if (mTimerUtil.isActive()) {
            mTimerUtil.cancel();
          }
          // 跳转home页面
          pushHomePage();
        },
        child: SizedBox(
          width: 55,
          height: 55,
          child: Stack(
            children: [
              Center(
                child: CircularProgressIndicator(
                  // 0.0-1.0
                  // 5-0
                  value: cirValue,
                ),
              ),
              Center(
                child: Text("${mTick.toInt()}s"),
              ),
            ],
          ),
        ),
      ),
    );
  }

  // 构建加载进度条
  buildLoadingProgress() {
    return !mIsShowBg
        ? Positioned(
            child: Center(
              child: SizedBox(
                width: 50,
                height: 50,
                child: CircularProgressIndicator(),
              ),
            ),
          )
        : Container();
  }

  // 构建logo
  buildLogo() {
    return Positioned(
      left: 0,
      right: 0,
      bottom: 80,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          ClipOval(
            child: Image.asset(
              "images/3.0/logo.jpg",
              width: 50,
              height: 50,
              fit: BoxFit.cover,
            ),
          ),
          SizedBox(width: 12),
          Text("Flutter-x Welcome!",
              style: TextStyle(
                color: Colors.blue,
                fontSize: 20,
                fontWeight: FontWeight.bold,
              ))
        ],
      ),
    );
  }
}
